package cqrtplm.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.hustcad.plm.pdm.classification.service.TyplmClassificationService;
import com.ty.basic.service.snow.SnowflakeIdComponent;
import cqrtplm.entity.CategoryPictureDO;
import cqrtplm.mapper.CategoryPictureMapper;
import cqrtplm.service.CategoryPictureService;
import cqrtplm.service.RTAttributeCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@Transactional
public class CategoryPictureServiceImpl implements CategoryPictureService {

    @Autowired
    private CategoryPictureMapper categoryPictureMapper;

    @Resource
    private RTAttributeCardService rtAttributeCardService;
    @Resource
    private SnowflakeIdComponent snowflakeIdComponent;
    @Resource
    private TyplmClassificationService typlmClassificationService;

    @Override
    public boolean save(CategoryPictureDO categoryPictureDO) {
        try {
            // 设置创建和更新时间
            LocalDateTime now = LocalDateTime.now();
            categoryPictureDO.setCreateStamp(now);
            categoryPictureDO.setUpdateStamp(now);

            return categoryPictureMapper.insert(categoryPictureDO) > 0;
        } catch (Exception e) {
            throw new RuntimeException("新增品类图片信息失败", e);
        }
    }

    @Override
    public CategoryPictureDO getByOid(String oid) {
        return categoryPictureMapper.selectByOid(oid);
    }

    @Override
    public CategoryPictureDO getByCategory(String category) {
        return categoryPictureMapper.selectByCategory(category);
    }

    @Override
    public PageInfo<CategoryPictureDO> listAll(Integer pageNum,Integer pageSize,String keyWork) {
        PageInfo<String> stringPageInfo = rtAttributeCardService.selectAllCode(pageNum, pageSize,keyWork);
        List<String> list = stringPageInfo.getList();
        if (CollUtil.isEmpty(list)) {
            PageInfo<CategoryPictureDO> pageInfo=new PageInfo<>();
            pageInfo.setList(Collections.emptyList());
            return pageInfo;
        }
        List<CategoryPictureDO> categoryPictureDOS = categoryPictureMapper.selectAll(list);
        List<CategoryPictureDO> newList = list
                .stream()
                .map(m -> {
                    CategoryPictureDO categoryPictureDO = new CategoryPictureDO();

                    Optional<CategoryPictureDO> any = categoryPictureDOS
                            .stream()
                            .filter(f -> f.getCategory().equals(m))
                            .findAny();
                    if (any.isPresent()) {
                        CategoryPictureDO c = any.get();
                        return c;
                    } else {
                        categoryPictureDO.setCategory(m);
                        categoryPictureDO.setPicture("否");
                    }
                    return categoryPictureDO;
                })
                .collect(Collectors.toList());
        PageInfo<CategoryPictureDO> pageInfo=new PageInfo<>();
        pageInfo.setList(newList);
        pageInfo.setTotal(stringPageInfo.getTotal());
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        return pageInfo;
    }

    @Override
    public boolean update(CategoryPictureDO categoryPictureDO) {
        try {
            if (StrUtil.isBlank(categoryPictureDO.getCategory())){
                return false;
            }
            if (StrUtil.isBlank(categoryPictureDO.getPicture())){
                return false;
            }
            CategoryPictureDO c = categoryPictureMapper.selectByCategory(
                    categoryPictureDO.getCategory());
            if (Objects.isNull(c)){
                CategoryPictureDO cpd=new CategoryPictureDO();
                cpd.setOid(snowflakeIdComponent.getInstance().nextId().toString());
                cpd.setCategory(categoryPictureDO.getCategory());
                cpd.setPicture(categoryPictureDO.getPicture());
                cpd.setCreateStamp(LocalDateTime.now());
                cpd.setUpdateStamp(LocalDateTime.now());
                categoryPictureMapper.insert(cpd);
                return true;
            }
            categoryPictureDO.setOid(c.getOid());
            // 设置更新时间
            categoryPictureDO.setUpdateStamp(LocalDateTime.now());
            return categoryPictureMapper.update(categoryPictureDO) > 0;
        } catch (Exception e) {
            throw new RuntimeException("更新品类图片信息失败", e);
        }
    }

    @Override
    public boolean removeByOid(String oid) {
        return categoryPictureMapper.deleteByOid(oid) > 0;
    }

    @Override
    public boolean removeByCategory(String category) {
        return categoryPictureMapper.deleteByCategory(category) > 0;
    }

    @Override
    public boolean batchSave(List<CategoryPictureDO> list) {
        try {
            LocalDateTime now = LocalDateTime.now();
            // 为每个记录设置时间
            list.forEach(item -> {
                item.setCreateStamp(now);
                item.setUpdateStamp(now);
            });

            return categoryPictureMapper.batchInsert(list) > 0;
        } catch (Exception e) {
            throw new RuntimeException("批量保存品类图片信息失败", e);
        }
    }

    @Override
    public String selectIsPicture(String category) {
        String code=null;
        if (StrUtil.isBlank(category)){
            return "否";
        }else {
            if (category.length()<5){
                return "否";
            }else {
                code = category.substring(0,5);
            }
        }
        CategoryPictureDO categoryPictureDO = categoryPictureMapper.selectByCategory(code);
        if (categoryPictureDO==null){
            return "否";
        }else {
            return categoryPictureDO.getPicture();
        }
    }
}